﻿using System;
using kad2SimSoft.Resources.Lokalisierung;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace kad2SimSoft
{
    /// <summary>
    /// Interaktionslogik für ProductionOrderSplitPopup.xaml
    /// </summary>
    public partial class ProductionOrderSplitPopup : Window
    {
        int _order1_amount;
        int _order2_amount;
        int _maxValue;
        bool canceled = false;

        public bool Canceled
        {
            get { return canceled; }
            set { canceled = value; }
        }

        public int Order1_amount
        {
            get { return _order1_amount; }
            set { _order1_amount = value; }
        }

        public int Order2_amount
        {
            get { return _order2_amount; }
            set { _order2_amount = value; }
        }

        public ProductionOrderSplitPopup(int maxValue)
        {
            InitializeComponent();
            _maxValue = maxValue;
            tbSplittedText.Text = kad2SimSoft.Properties.Resources.lbSplitText + " " + _maxValue.ToString();
            tbOrder1Amount.Text = maxValue.ToString();
            tbOrder2Amount.Text = "0";
        }

        private void Button_Save_Click(object sender, RoutedEventArgs e)
        {
            _order1_amount = Convert.ToInt32(tbOrder1Amount.Text);
            _order2_amount = Convert.ToInt32(tbOrder2Amount.Text);

            if (_order1_amount == 0 || _order2_amount == 0)
            {
                MessageBox.Show(kad2SimSoft.Properties.Resources.msgAmountsNotCorrect);
            }
            else
            {
                canceled = false;
                this.Close();
            }
        }

        private void tbOrder1Amount_KeyUp(object sender, KeyEventArgs e)
        {
            if (tbOrder1Amount.Text.Equals("") || tbOrder1Amount.Text.Equals("0") || (!isValueNumeric(tbOrder1Amount.Text))) { tbOrder1Amount.Text = "1"; }
            if (Convert.ToInt32(tbOrder1Amount.Text) > _maxValue) { tbOrder1Amount.Text = _maxValue.ToString(); }
            tbOrder2Amount.Text = (_maxValue - Convert.ToInt32(tbOrder1Amount.Text)).ToString();
        }

        private void tbOrder2Amount_KeyUp(object sender, KeyEventArgs e)
        {
            if (tbOrder2Amount.Text.Equals("") || tbOrder1Amount.Text.Equals("0") || (!isValueNumeric(tbOrder2Amount.Text))) { tbOrder2Amount.Text = "1"; }
            if (Convert.ToInt32(tbOrder2Amount.Text) > _maxValue) { tbOrder2Amount.Text = _maxValue.ToString(); }
            tbOrder1Amount.Text = (_maxValue - Convert.ToInt32(tbOrder2Amount.Text)).ToString();
        }

        private void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            canceled = true;
            this.Close();
        }
        private static bool isValueNumeric(string text)
        {
            Regex regex = new Regex("[^0-9.]+"); //regex that matches disallowed text
            return !regex.IsMatch(text);
        }
    }
}
